package com.huifaapp.mall.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.Homefragment.GlideCacheUtil;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.VersonBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

public class PersonSetActivity extends AppCompatActivity {

    private LinearLayout Personset;
    private LinearLayout RecevieAddress;
    private ImageView Imback;
    private TextView tv_qchc;
    private LinearLayout tv_qchc_a;
    private Button bt_exit;
    private TextView tv_verson;
    private VersonBean versonBean;
    private VersonBean.DataBean dataBean;
    private LinearLayout ll_guanyuwomen;
    private  LinearLayout ll_guanlianwx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_set);

        initView();
        initData();

    }



    private void initData() {

    }

    private void initView() {
        Personset = (LinearLayout) findViewById(R.id.LinearLayout_personset);
        RecevieAddress = (LinearLayout) findViewById(R.id.LinearLayout_receive);
        Imback = (ImageView) findViewById(R.id.iv_personset_back);
        tv_qchc = (TextView) findViewById(R.id.person_set_qchc);
        tv_qchc_a = (LinearLayout) findViewById(R.id.person_set_qchc_a);
        bt_exit = (Button) findViewById(R.id.person_set_exit);
        tv_verson = (TextView) findViewById(R.id.person_set_version);
        ll_guanyuwomen = (LinearLayout) findViewById(R.id.ll_guanyuwomen);
        ll_guanlianwx= (LinearLayout) findViewById(R.id.ll_guanlianwx);
        ll_guanlianwx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OkHttpUtils.post().url(ApiManager.USERS_BINDWX)
                        .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                        .addParams("appId", AUniqueIdentifier.getDeviceId(PersonSetActivity.this))
                        .addParams("userNickName", SPUtils.getInstance().getString(SPConstants.NAME))
                        .addParams("userHeadImg",SPUtils.getInstance().getString(SPConstants.MAVATR))
                        .addParams("unionid", SPUtils.getInstance().getString(SPConstants.UNIONID))
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {

                            }

                            @Override
                            public void onResponse(String response) {
                                try {
                                    JSONObject obj=new JSONObject(response);
                                    if(obj.getString("code").equals("10000")){
                                        Toast.makeText(PersonSetActivity.this, obj.getString("msg"), Toast.LENGTH_SHORT).show();
                                    }else{
                                        Toast.makeText(PersonSetActivity.this, obj.getString("msg"), Toast.LENGTH_SHORT).show();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });

            }
        });
        ll_guanyuwomen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PersonSetActivity.this,RankingRanNo1HtmlActivity.class);
                intent.putExtra("url","http://www.uhuijia.com.cn/aboutus.html");
                intent.putExtra("title","关于我们");
                startActivity(intent);
            }
        });
        Imback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        Personset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PersonSetActivity.this,PersonalProfileActivity.class);
                startActivity(intent);
            }
        });
        RecevieAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentadd = new Intent(PersonSetActivity.this,ReceiveAddressActivity.class);
                startActivity(intentadd);
            }
        });

        tv_qchc.setText(GlideCacheUtil.getCacheSize(PersonSetActivity.this));
        tv_qchc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog alertDialog = new AlertDialog.Builder(PersonSetActivity.this).setTitle("提示").setMessage("是否需要删除？")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                GlideCacheUtil.clearImageAllCache(PersonSetActivity.this);
                                tv_qchc.setText("0.0KB");
                                ToastUtils.showShort("清除完成");
                            }
                        }).create();
                alertDialog.show();
            }
        });
        bt_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog alertDialog = new AlertDialog.Builder(PersonSetActivity.this).setTitle("提示").setMessage("确定退出登陆吗？")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                OkHttpUtils.post().url(ApiManager.BASE_NIUPAI_URL+"login/out")
                                        .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                                        .addParams("appId", AUniqueIdentifier.getDeviceId(PersonSetActivity.this))
                                        .build()
                                        .execute(new StringCallback() {
                                            @Override
                                            public void onError(Request request, Exception e) {

                                            }

                                            @Override
                                            public void onResponse(String response) {
                                                ToastUtils.showShort("退出成功");
                                                SPUtils.getInstance().put(SPConstants.KEY_USER_TOKEN,"");
                                                SPUtils.getInstance().put(SPConstants.KEY_ISSTORE,"0");
                                                finish();
                                            }
                                        });


                            }
                        }).create();
                alertDialog.show();
            }
        });


        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"login/in")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {

                        try {
                            Gson gson = new Gson();
                            versonBean = gson.fromJson(response,VersonBean.class);
                            dataBean = versonBean.getData();
                            tv_verson.setText(versonBean.getData().getVersion());
                        }catch (Exception e){
                            e.printStackTrace();
                        }



                    }
                });
    }
}
